package cn.xiuxius.embedded.comdev;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SerialPortDataListenerImpl implements SerialPortDataListener {


    private final MessageDispatcher dispatcher;

    public SerialPortDataListenerImpl(MessageDispatcher dispatcher) {
        this.dispatcher = dispatcher;

    }
    @Override
    public int getListeningEvents() {
        return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
    }

    @Override
    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_RECEIVED) {
            dispatcher.dispatchReceivedMessage(event.getReceivedData());
        }
    }

}
